package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.OrdbyModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet("/back/user/findAllBySon")
public class FindAllBySonServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //1.处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//前台拿值
        String username=req.getParameter("username");
        String startBrithday=req.getParameter("startBrithday");
        String endBrithday=req.getParameter("endBrithday");
        String introduce=req.getParameter("introduce");
        String hobby=req.getParameter("hobby");
        hobby=hobby.replace("[","")
                .replace("]","")
                .replaceAll(" ","")
                .replaceAll("\"","");
        String sex=req.getParameter("sex");
        String phone=req.getParameter("phone");

        String ordbyid=req.getParameter("ordbyid");
        String ordbyname=req.getParameter("ordbyname");
        String ordbyphone=req.getParameter("ordbyphone");
        String ordbysex=req.getParameter("ordbysex");
        String ordbybirthday=req.getParameter("ordbybirthday");

        String pageNum=req.getParameter("pageNum");
        String pageSize=req.getParameter("pageSize");


        //3.封装到实体类UserModel
        OrdbyModel ordbyModel=new OrdbyModel();
        ordbyModel.setOrdbyid(ordbyid);
        ordbyModel.setOrdbyname(ordbyname);
        ordbyModel.setOrdbyphone(ordbyphone);
        ordbyModel.setOrdbysex(ordbysex);
        ordbyModel.setOrdbybirthday(ordbybirthday);

        //封装
        PageModel pageModel=new PageModel();
        pageModel.setPageNum(pageNum==null || "".equals(pageNum)? 1 :Integer.parseInt(pageNum));
        pageModel.setPageSize(pageSize==null || "".equals(pageSize) ? 10:Integer.parseInt(pageSize));

        UserModel userModel=new UserModel();
        userModel.setUsername(username);
        userModel.setStartBrithday(startBrithday==null?"":startBrithday);
        userModel.setEndBrithday(endBrithday==null?"":endBrithday);
        userModel.setIntroduce(introduce);
        userModel.setHobby(hobby);
        userModel.setSex(sex);
        userModel.setPhone(phone);
        userModel.setPageModel(pageModel);
        userModel.setOrdbyModel(ordbyModel);

        String hobbys[]=hobby.split(",");
        List<String> hobbyList=new ArrayList<>();
        hobbyList.addAll(Arrays.asList(hobbys));
        userModel.setHobbyList(hobbyList);

        //调用服务逻辑层
        IUserService iUserService=new UserServiceImpl();

        //返回数据
        List<UserModel> list=iUserService.findAllBySon(userModel);
        int number=iUserService.getCount(userModel);

        //封装json
        //6.将数据转发到页面
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("number",number);
        jsonObject.put("list",list);
        resp.getWriter().println(jsonObject);

    }
}
